package org.check.UI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.border.EtchedBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import org.check.Util.LoadUtil;

import com.jgoodies.common.collect.LinkedListModel;

import net.miginfocom.swing.MigLayout;

public class ColSelectPanel extends JPanel {

	private JList list_selectCol;
	private JTextField text_searchCol;
	Map<String, String> nameArrtMap = LoadUtil.getNameAttrMap();
	/**
	 * Create the 
	 */
	public ColSelectPanel() {
		setBackground(new Color(192, 192, 192));
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setLayout(new MigLayout("", "[90.00px][90.00px]", "[23px][21px][466.00px]"));

		JScrollPane scrollPane_1 = new JScrollPane();
		add(scrollPane_1, "cell 0 2 2 1,grow");

		list_selectCol = new JList();
		list_selectCol.setToolTipText("按住Ctrl选择多个数据");

		scrollPane_1.setViewportView(list_selectCol);

		list_selectCol.setBackground(new Color(240, 255, 255));
		ListModel listModel = new LinkedListModel<String>(nameArrtMap.keySet());
		list_selectCol.setModel(listModel);
		
		
		JButton button_selectAll = new JButton("全选");
		button_selectAll.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				list_selectCol.clearSelection();
				list_selectCol.setSelectionInterval(0, list_selectCol.getModel().getSize() - 1);
			}
		});
		add(button_selectAll, "cell 0 0,growx,aligny top");

		JButton button_selectOppo = new JButton("反选");
		button_selectOppo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int listSize = list_selectCol.getModel().getSize();
				int[] newIndices = new int[listSize - list_selectCol.getSelectedIndices().length];
				for (int j = 0, k = 0; j < listSize; ++j)
					if (!list_selectCol.isSelectedIndex(j))
						newIndices[k++] = j;
				list_selectCol.clearSelection();
				list_selectCol.setSelectedIndices(newIndices);
			}
		});
		add(button_selectOppo, "cell 1 0,growx,aligny top");

		text_searchCol = new JTextField();
		text_searchCol.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				list_selectCol.clearSelection();
				String text = text_searchCol.getText();
				for (int i = 0; i < list_selectCol.getModel().getSize(); i++) {
					if (((String) list_selectCol.getModel().getElementAt(i)).contains(text)) {
						list_selectCol.addSelectionInterval(i, i);
					}
				}
			}
		});
		add(text_searchCol, "cell 0 1 2 1,growx,aligny top");
	}

	public List<String> getCheckedElements() {
		List<String> result = new LinkedList<String>();
		Object[] eles = list_selectCol.getSelectedValues();
		for (int i = 0; i < eles.length; i++) {
			String temp = eles[i].toString();
			result.add(temp);
		}
		return result;
	}
}
